<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends Controller {
    
    //添加商品
	public function add_goods(){

		if(IS_AJAX){

			$data= $_POST;

			$data['add_time'] = time();
			$data['update_time'] = time();
			$data['status'] = 1;

			$res = M('goods')->add($data);

			//
			if($res>0){
				$json = array('status'=>1,'msg'=>'添加成功');
			}
			else{
				$json = array('status'=>0,'msg'=>'添加失败');
			}
			
			$this->ajaxReturn($json);
		}
		else{
			$this->display();
		}

	}

    
    //商品列表
	public function goods_list(){

		if(IS_AJAX){

			//$condition = array('status'=>array('neq',-1));
			$condition['status'] = array('neq',-1);

			$data = M('goods')->where($condition)->select();  //前台显示隐藏status = -1 的数据

			$this->ajaxReturn($data);



		}
		else{
			$this->display();
		}	

	}


    //删除
	public function change_status(){
		
		$id = $_POST['id'];
		$status = $_POST['status'];

		$res = M('goods')->where(array('id'=>$id))->save(array('status'=>$status));

		
		if($res > 0){
			$this->ajaxReturn(array('status'=>1,'msg'=>'操作成功'));
		}
		else{

			$this->ajaxReturn(array('status'=>0,'msg'=>'操作失败'));
		}
		   
		// $res = M('goods')->where(array('status'=>-1))->delete();

		//判断是否修改成功

	}


   

	// public function goods_list2(){

	// 	$data = M('goods')->select();
	// 	$this->assign('data',$data);
	// 	$this->display();
	// }
	


	//修改商品信息
	public function update_info(){

		if(IS_AJAX){
			
			$data = $_POST;
			unset($data['id']);
			//返回的事影响行数
			$res = M('goods')->where(array('id'=>$_POST['id']))->save($data);	

			if($res > 0){
				$this->ajaxReturn(array('status'=>1,'msg'=>'修改成功'));
			}
			else{

				$this->ajaxReturn(array('status'=>0,'msg'=>'修改失败或没有任何修改'));
			}



		}
		else{
	        $id = I('id');
	        //查找到某条数据
	        $arr = array('id'=>$id);
			$res = M('goods')->where($arr)->find();

	        
		    //注入变量，分配变量
			$this->assign('info',$res);
			//渲染模板
	        $this->display();
		}
	}

}